<?PHP
/**
 *    
 */
class Admin_field_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','溯源扩展字段');
		$this->assign('menuid1','6');
		$this->assign('menuid2','5'); 
		//权限判断
		factory::module('admin')->authSys('source_setup'); 
	}
	
	##################################
	/* 溯源扩展字段  */
	##################################
	function index__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';    
  	  #查询条件
			$sql_where = ' where 0=0 '.func::speedsearch($_POST['keywords'],array( 'a.title','a.content' ));
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_field_who` a  
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.id, 
					a.name
				from `t_field_who` a 
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				foreach($rs as &$v){
					$v['field_num'] = $db->loadresult('select count(*) from t_field where who="'.$v['id'].'" ');
				}
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}  
	} 
	
	
	##################################
	/* 修改某个类型的自定义字段  */
	##################################
	public function  modify__(){ 
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs_who = $db->loadassoc('select a.* from `t_field_who` a  where a.id="'.$id.'" ');
		if(!$rs_who) exit('没有查到任何记录，请重新操作！'); 
		$this->assign('rs_who',$rs_who);
		
		$field_type = $this->get_fields();
		$this->assign('field_type',$field_type);
		
		$rs = $db->loadassoclist('select * from t_field where who="'.$rs_who[id].'" order by show_index asc ','id');
		$this->assign('rs',$rs);
		
	}
	
	
	##################################
	/* 自定义字段排序  */
	##################################
	public function modify_field_showindex__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$groupindex = format::addslash_json(json_decode( stripslashes($_POST['groupindex']) ));
		
			$db = factory::getdbo();
			$db->begin();  
			if(is_array($groupindex)){
				foreach($groupindex as &$v){ 
					if(!$db->query('update t_field set show_index="'.$v['show_index'].'" where id="'.$v['id'].'" ')){
						$db->rollback();
						exit('{"success":0, "msg":"服务器开小差了，请稍后再尝试！"}');
					}					
				}
			}
			$db->commit();
			exit('{"success":"1"}');
		} 
	}
	##################################
	/* 删除自定义字段  */
	##################################
	public function del__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$_POST=format::addslash_deep($_POST);
			$id = intval($_POST['id']);
			
			$db = factory::getdbo();
			$db->begin();  
  		
			# 删除
			$db->query("delete from t_field where id='".$id."' ");
			$db->query("delete from t_field_option where field_id='".$id."' ");
			$db->query("delete from t_field_user_val where field_id='".$id."' ");
			$db->query("delete from t_field_vege_val where field_id='".$id."' ");
			
			$db->commit();
			exit('{"success":"1"}');
		}  
	}
	

	##################################
	/* 添加自定义字段  */
	##################################
	public function  add__(){ 
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs_who = $db->loadassoc('select a.* from `t_field_who` a  where a.id="'.$id.'" ');
		if(!$rs_who) exit('没有查到任何记录，请重新操作！'); 
		$this->assign('rs_who',$rs_who);
		
		$field = $this->get_fields();
		$this->assign('field',$field);
	}
	public function addsave__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
			$_POST=format::addslash_deep($_POST);
			$who = intval($_POST['who']);
			
			$db = factory::getdbo();
			$db->begin();
			$field_id = $db->insert('field',array(
				'who'=>$who,
				'field_name'=>$form['field_name'],
				'field_type'=>$form['field_type'],
				'field_length'=>$form['field_length'],
				'field_decimal_len'=>$form['field_decimal_len']
			));
			if($form['field_type'] == 'slt' || $form['field_type'] == 'mslt'){
				$order   = array("\r\n", "\r");   
				$field_sel_val = str_replace($order, "\n", $form['field_option_val']);
				$field_sel_array = explode("\n", $field_sel_val);
				$kk = 1;
				foreach ($field_sel_array as $v){
						if ($v != ''){
								$dd = array('field_id'=>$field_id, 'val'=>$v, 'show_index'=>$kk);
								$db->insert('field_option', $dd);
								$kk++;
						}
				}
			}
			$db->commit();
			exit('{"success":"1"}');
		}
		exit();
	}
	
	
	
	
	##################################
	/* 修改自定义字段  */
	##################################
	public function  modify_field__(){ 
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_field` a  where a.id="'.$id.'" ');
		if(!$rs) exit('没有查到任何记录，请重新操作！'); 
		$this->assign('rs',$rs);
		
		
		$rs_option = array();
		if($rs['field_type'] == 'slt' || $rs['field_type'] == 'mslt'){
			$rs_option = $db->loadassoclist('select val from t_field_option where field_id="'.$rs['id'].'" order by show_index asc ','val');
			$rs_option = array_keys($rs_option);
		}
		foreach ($rs_option as $v)
		{
				$rs_option_val .= $v . '\r\n';
		}
		$this->assign('rs_option',$rs_option_val);
		
		$field = $this->get_fields();
		$this->assign('field',$field);
	}
	public function modify_field_save__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
			$_POST=format::addslash_deep($_POST);
			$id = intval($_POST['id']);
			
			$db = factory::getdbo();
			$db->begin();
			$db->update('field',array(
				'id'=>$id,
				'field_name'=>$form['field_name'],
				'field_type'=>$form['field_type'],
				'field_length'=>$form['field_length'],
				'field_decimal_len'=>$form['field_decimal_len']
			));
			$db->query('delete from t_field_option where field_id="'.$id.'" ');
			if($form['field_type'] == 'slt' || $form['field_type'] == 'mslt'){
				$order   = array("\r\n", "\r");   
				$field_sel_val = str_replace($order, "\n", $form['field_option_val']);
				$field_sel_array = explode("\n", $field_sel_val);
				$kk = 1;
				foreach ($field_sel_array as $v){
						if ($v != ''){
								$dd = array('field_id'=>$id, 'val'=>$v, 'show_index'=>$kk);
								$db->insert('field_option', $dd);
								$kk++;
						}
				}
			}
			$db->commit();
			exit('{"success":"1"}');
		}
		exit();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 字段类型 
	 */
	private function get_fields()
	{
	    $field = array();
        $field['char'] = array('type'=>'char', 'img'=>'/misc/images/field/text.gif', 'text'=>'文本字段', 'length'=>'3');
       // $field['date'] = array('type'=>'date', 'img'=>'/misc/images/field/date.gif', 'text'=>'日期', 'length'=>'3');
        $field['int'] = array('type'=>'int', 'img'=>'/misc/images/field/number.gif', 'text'=>'整数', 'length'=>'1');
        $field['float'] = array('type'=>'float', 'img'=>'/misc/images/field/decimal.gif', 'text'=>'小数', 'length'=>'1');
        $field['per'] = array('type'=>'per', 'img'=>'/misc/images/field/percent.gif', 'text'=>'百分数', 'length'=>'1');
        $field['slt'] = array('type'=>'slt', 'img'=>'/misc/images/field/picklist.gif', 'text'=>'选择列表', 'length'=>'3');
        //$field['mslt'] = array('type'=>'mslt', 'img'=>'/misc/images/field/multiselect.gif', 'text'=>'多选列表', 'length'=>'3');
        
        return $field;
	}
	
	
}
?>